using Easislides.Properties;
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace Easislides
{
    public class FrmManageItemLists : Form
    {
        private string InputDir;
        private string InputDefaultFile;
        private string ListText;
        private string ListTextTo;
        private string CurFile;
        private string StartingFolder;
        private string Ext_ToUse = "";
        private IContainer components = null;
        private ListView ItemList;
        private Button AddBtn;
        private Button RenameBtn;
        private Button DelBtn;
        private Button CloseBtn;
        private ColumnHeader columnHeader1;
        private Button SaveAsBtn;
        private Button SaveToBtn;
        private Button SaveTemplateBtn;
        private SaveFileDialog saveFileDialog1;
        public FrmManageItemLists()
        {
            this.InitializeComponent();
        }
        private void FrmManageItemLists_Load(object sender, EventArgs e)
        {
            CloseBtn.DialogResult = DialogResult.Cancel;
            if (gf.EasiSlidesMode == gf.UsageMode.Worship)
            {
                this.Text = "Manage Worship Lists";
                this.InputDir = gf.WorshipDir;
                this.InputDefaultFile = "Worship Service";
                this.CurFile = gf.CurSession;
                this.ListText = "WorshipList";
                this.ListTextTo = "PraiseBook";
            }
            else
            {
                if (gf.EasiSlidesMode == gf.UsageMode.PraiseBook)
                {
                    this.Text = "Manage PraiseBooks";
                    this.InputDir = gf.PraiseBookDir;
                    this.InputDefaultFile = "PraiseBook 1";
                    this.CurFile = gf.CurPraiseBook;
                    this.ListText = "PraiseBook";
                    this.ListTextTo = "WorshipList";
                }
            }
            this.SaveToBtn.Text = this.ListTextTo;
            this.UpdateLists();
        }
        private void UpdateLists()
        {
            bool flag = false;
            byte[] array = new byte[1];
            this.ItemList.Items.Clear();
            gf.ValidateDir(this.InputDir, true);
            this.Ext_ToUse = ((this.InputDir == gf.WorshipDir) ? ".esw" : ".esp");
            string[] files = Directory.GetFiles(this.InputDir, "*" + this.Ext_ToUse);
            string[] array2 = files;
            for (int i = 0; i < array2.Length; i++)
            {
                string text = array2[i];
                string text2 = text;
                text2 = gf.GetDisplayNameOnly(ref text2, true);
                if (text2 != "")
                {
                    this.ItemList.Items.Add(text2);
                    if (this.CurFile == text2)
                    {
                        flag = true;
                    }
                }
            }
            if (flag)
            {
                for (int j = 0; j < this.ItemList.Items.Count; j++)
                {
                    if (this.CurFile == this.ItemList.Items[j].Text)
                    {
                        this.ItemList.Items[j].Selected = true;
                        break;
                    }
                }
            }
            else
            {
                if (gf.EasiSlidesMode == gf.UsageMode.Worship)
                {
                    if (this.ItemList.Items.Count > 0)
                    {
                        gf.CurSession = this.ItemList.Items[0].Text;
                    }
                    else
                    {
                        gf.CurSession = "";
                    }
                }
                else
                {
                    if (gf.EasiSlidesMode == gf.UsageMode.PraiseBook)
                    {
                        if (this.ItemList.Items.Count > 0)
                        {
                            gf.CurPraiseBook = this.ItemList.Items[0].Text;
                        }
                        else
                        {
                            gf.CurPraiseBook = "";
                        }
                    }
                }
            }
        }
        private void AddBtn_Click(object sender, EventArgs e)
        {
            gf.NameChangeAction = 1;
            gf.NameChangeSucceeded = false;
            FrmUpdateFileName frmUpdateFileName = new FrmUpdateFileName();
            if (frmUpdateFileName.ShowDialog() == DialogResult.OK)
            {
                this.UpdateLists();
                if (gf.EasiSlidesMode == gf.UsageMode.Worship)
                {
                    gf.WorshipListsChanged = true;
                }
                else
                {
                    if (gf.EasiSlidesMode == gf.UsageMode.PraiseBook)
                    {
                        gf.PraiseBooksListChanged = true;
                    }
                }
            }
        }
        private void DelBtn_Click(object sender, EventArgs e)
        {
            if (this.ItemList.SelectedItems.Count < 1)
            {
                MessageBox.Show("You have not selected a " + this.ListText + " to delete!");
            }
            else
            {
                string text = this.ItemList.SelectedItems[0].Text;
                if (MessageBox.Show(string.Concat(new string[]
				{
					"Really delete '",
					text,
					"' ",
					this.ListText,
					"?"
				}), "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    try
                    {
                        File.Delete(this.InputDir + text + this.Ext_ToUse);
                        this.UpdateLists();
                        if (gf.EasiSlidesMode == gf.UsageMode.Worship)
                        {
                            gf.WorshipListsChanged = true;
                        }
                        else
                        {
                            if (gf.EasiSlidesMode == gf.UsageMode.PraiseBook)
                            {
                                gf.PraiseBooksListChanged = true;
                            }
                        }
                    }
                    catch
                    {
                        MessageBox.Show("Error encountered whilst trying to delete the " + this.ListText + " - Please ensure you have delete access.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                }
            }
        }
        private void RenameBtn_Click(object sender, EventArgs e)
        {
            gf.NameChangeAction = 2;
            if (this.ItemList.SelectedItems.Count < 1)
            {
                MessageBox.Show("You have not selected a " + this.ListText + " to rename!");
            }
            else
            {
                gf.SelectedListName = this.ItemList.SelectedItems[0].Text;
                bool flag = gf.SelectedListName == this.CurFile;
                FrmUpdateFileName frmUpdateFileName = new FrmUpdateFileName();
                if (frmUpdateFileName.ShowDialog() == DialogResult.OK)
                {
                    if (flag)
                    {
                        this.CurFile = gf.SelectedListName;
                    }
                    if (gf.EasiSlidesMode == gf.UsageMode.Worship)
                    {
                        gf.WorshipListsChanged = true;
                        gf.CurSession = this.CurFile;
                    }
                    else
                    {
                        gf.PraiseBooksListChanged = true;
                        gf.CurPraiseBook = this.CurFile;
                    }
                    this.UpdateLists();
                }
            }
        }
        private void SaveAsBtn_Click(object sender, EventArgs e)
        {
            if (this.ItemList.SelectedItems.Count < 1)
            {
                MessageBox.Show("You have not selected a " + this.ListText + " to Save As!");
            }
            else
            {
                string text = this.ItemList.SelectedItems[0].Text;
                if (this.SaveAsItem(this.InputDir + text + this.Ext_ToUse) != "")
                {
                    if (gf.EasiSlidesMode == gf.UsageMode.Worship)
                    {
                        gf.WorshipListsChanged = true;
                    }
                    else
                    {
                        if (gf.EasiSlidesMode == gf.UsageMode.PraiseBook)
                        {
                            gf.PraiseBooksListChanged = true;
                        }
                    }
                }
            }
        }
        private void SaveToBtn_Click(object sender, EventArgs e)
        {
            if (this.ItemList.SelectedItems.Count < 1)
            {
                MessageBox.Show("You have not selected a " + this.ListText + " to Save To " + this.ListTextTo);
            }
            else
            {
                string text = this.ItemList.SelectedItems[0].Text;
                gf.SelectedListName = this.InputDir + text + this.Ext_ToUse;
                FrmUpdateFileName frmUpdateFileName = new FrmUpdateFileName();
                gf.NameChangeSucceeded = false;
                if (gf.EasiSlidesMode == gf.UsageMode.Worship)
                {
                    gf.NameChangeAction = 7;
                    if (frmUpdateFileName.ShowDialog() == DialogResult.OK)
                    {
                        gf.PraiseBooksListChanged = true;
                    }
                }
                else
                {
                    gf.NameChangeAction = 8;
                    if (frmUpdateFileName.ShowDialog() == DialogResult.OK)
                    {
                        gf.WorshipListsChanged = true;
                    }
                }
            }
        }
        private void SaveTemplateBtn_Click(object sender, EventArgs e)
        {
            if (this.ItemList.SelectedItems.Count < 1)
            {
                MessageBox.Show("You have not selected a " + this.ListText + " to Save As a Template!");
            }
            else
            {
                string text = this.ItemList.SelectedItems[0].Text;
                try
                {
                    string text2 = this.InputDir + text + this.Ext_ToUse;
                    string text3 = gf.WorshipTemplatesDir + text + ".est";
                    if (File.Exists(text2))
                    {
                        DialogResult dialogResult = DialogResult.Yes;
                        if (File.Exists(text3))
                        {
                            dialogResult = MessageBox.Show("There is already a template with the same name. Overwrite it?", "Overwrite", MessageBoxButtons.YesNo);
                        }
                        if (dialogResult == DialogResult.Yes)
                        {
                            File.Copy(text2, text3, true);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Error Saving Template - cannot find the selected " + this.ListText + "!");
                    }
                }
                catch
                {
                    MessageBox.Show("Error Saving File, please make sure you have write access and try again");
                }
            }
        }
        private string SaveAsItem(string InFileName)
        {
            this.saveFileDialog1.Filter = string.Concat(new string[]
			{
				"EasiSlides ",
				this.ListText,
				" File (*",
				this.Ext_ToUse,
				")|*",
				this.Ext_ToUse
			});
            this.saveFileDialog1.InitialDirectory = Path.GetDirectoryName(InFileName);
            this.saveFileDialog1.FileName = gf.GetDisplayNameOnly(ref InFileName, false, true);
            this.saveFileDialog1.OverwritePrompt = true;
            this.saveFileDialog1.AddExtension = true;
            this.saveFileDialog1.DefaultExt = this.Ext_ToUse;
            string result;
            if (this.saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = this.saveFileDialog1.FileName;
                try
                {
                    if (!File.Exists(fileName))
                    {
                        File.Copy(InFileName, fileName, true);
                        this.UpdateLists();
                        result = fileName;
                        return result;
                    }
                }
                catch
                {
                    MessageBox.Show("Error Saving File, please make sure you have write access and try again");
                }
            }
            result = "";
            return result;
        }
        private void CloseBtn_Click(object sender, EventArgs e)
        {
            this.MakeSelectedItemCurrent();
        }
        private void ItemList_DoubleClick(object sender, EventArgs e)
        {
            this.MakeSelectedItemCurrent();
            base.Close();
        }
        private void MakeSelectedItemCurrent()
        {
            if (this.ItemList.SelectedItems.Count > 0)
            {
                if (gf.EasiSlidesMode == gf.UsageMode.Worship)
                {
                    gf.CurSession = this.ItemList.SelectedItems[0].Text;
                }
                else
                {
                    gf.CurPraiseBook = this.ItemList.SelectedItems[0].Text;
                }
            }
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing && this.components != null)
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }
        private void InitializeComponent()
        {
            this.ItemList = new System.Windows.Forms.ListView();
            this.columnHeader1 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
            this.CloseBtn = new System.Windows.Forms.Button();
            this.SaveToBtn = new System.Windows.Forms.Button();
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            this.SaveTemplateBtn = new System.Windows.Forms.Button();
            this.SaveAsBtn = new System.Windows.Forms.Button();
            this.DelBtn = new System.Windows.Forms.Button();
            this.RenameBtn = new System.Windows.Forms.Button();
            this.AddBtn = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // ItemList
            // 
            this.ItemList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader1});
            this.ItemList.FullRowSelect = true;
            this.ItemList.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.None;
            this.ItemList.HideSelection = false;
            this.ItemList.Location = new System.Drawing.Point(12, 12);
            this.ItemList.Name = "ItemList";
            this.ItemList.ShowGroups = false;
            this.ItemList.ShowItemToolTips = true;
            this.ItemList.Size = new System.Drawing.Size(181, 200);
            this.ItemList.Sorting = System.Windows.Forms.SortOrder.Ascending;
            this.ItemList.TabIndex = 0;
            this.ItemList.UseCompatibleStateImageBehavior = false;
            this.ItemList.View = System.Windows.Forms.View.Details;
            this.ItemList.DoubleClick += new System.EventHandler(this.ItemList_DoubleClick);
            // 
            // columnHeader1
            // 
            this.columnHeader1.Text = "";
            this.columnHeader1.Width = 165;
            // 
            // CloseBtn
            // 
            this.CloseBtn.Location = new System.Drawing.Point(105, 220);
            this.CloseBtn.Name = "CloseBtn";
            this.CloseBtn.Size = new System.Drawing.Size(88, 24);
            this.CloseBtn.TabIndex = 4;
            this.CloseBtn.Text = "Close";
            this.CloseBtn.Click += new System.EventHandler(this.CloseBtn_Click);
            // 
            // SaveToBtn
            // 
            this.SaveToBtn.Image = global::Properties.Resources.list_numbers;
            this.SaveToBtn.Location = new System.Drawing.Point(199, 188);
            this.SaveToBtn.Name = "SaveToBtn";
            this.SaveToBtn.Size = new System.Drawing.Size(88, 24);
            this.SaveToBtn.TabIndex = 6;
            this.SaveToBtn.Text = "WorshipList";
            this.SaveToBtn.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.SaveToBtn.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            this.SaveToBtn.Click += new System.EventHandler(this.SaveToBtn_Click);
            // 
            // SaveTemplateBtn
            // 
            this.SaveTemplateBtn.Image = global::Properties.Resources.template;
            this.SaveTemplateBtn.Location = new System.Drawing.Point(199, 158);
            this.SaveTemplateBtn.Name = "SaveTemplateBtn";
            this.SaveTemplateBtn.Size = new System.Drawing.Size(88, 24);
            this.SaveTemplateBtn.TabIndex = 7;
            this.SaveTemplateBtn.Text = "Template";
            this.SaveTemplateBtn.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            this.SaveTemplateBtn.Click += new System.EventHandler(this.SaveTemplateBtn_Click);
            // 
            // SaveAsBtn
            // 
            this.SaveAsBtn.Image = global::Properties.Resources.saveas;
            this.SaveAsBtn.Location = new System.Drawing.Point(199, 128);
            this.SaveAsBtn.Name = "SaveAsBtn";
            this.SaveAsBtn.Size = new System.Drawing.Size(88, 24);
            this.SaveAsBtn.TabIndex = 5;
            this.SaveAsBtn.Text = "Save As";
            this.SaveAsBtn.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            this.SaveAsBtn.Click += new System.EventHandler(this.SaveAsBtn_Click);
            // 
            // DelBtn
            // 
            this.DelBtn.Image = global::Properties.Resources.deleteW;
            this.DelBtn.Location = new System.Drawing.Point(199, 72);
            this.DelBtn.Name = "DelBtn";
            this.DelBtn.Size = new System.Drawing.Size(88, 24);
            this.DelBtn.TabIndex = 3;
            this.DelBtn.Text = "Delete";
            this.DelBtn.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            this.DelBtn.Click += new System.EventHandler(this.DelBtn_Click);
            // 
            // RenameBtn
            // 
            this.RenameBtn.Image = global::Properties.Resources.rename;
            this.RenameBtn.Location = new System.Drawing.Point(199, 42);
            this.RenameBtn.Name = "RenameBtn";
            this.RenameBtn.Size = new System.Drawing.Size(88, 24);
            this.RenameBtn.TabIndex = 2;
            this.RenameBtn.Text = "Rename";
            this.RenameBtn.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            this.RenameBtn.Click += new System.EventHandler(this.RenameBtn_Click);
            // 
            // AddBtn
            // 
            this.AddBtn.Image = global::Properties.Resources.new1;
            this.AddBtn.Location = new System.Drawing.Point(199, 12);
            this.AddBtn.Name = "AddBtn";
            this.AddBtn.Size = new System.Drawing.Size(88, 24);
            this.AddBtn.TabIndex = 1;
            this.AddBtn.Text = "Add New";
            this.AddBtn.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageBeforeText;
            this.AddBtn.Click += new System.EventHandler(this.AddBtn_Click);
            // 
            // FrmManageItemLists
            // 
            this.ClientSize = new System.Drawing.Size(299, 256);
            this.Controls.Add(this.SaveTemplateBtn);
            this.Controls.Add(this.SaveToBtn);
            this.Controls.Add(this.SaveAsBtn);
            this.Controls.Add(this.CloseBtn);
            this.Controls.Add(this.DelBtn);
            this.Controls.Add(this.RenameBtn);
            this.Controls.Add(this.AddBtn);
            this.Controls.Add(this.ItemList);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FrmManageItemLists";
            this.ShowIcon = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Load += new System.EventHandler(this.FrmManageItemLists_Load);
            this.ResumeLayout(false);

        }
    }
}
